/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.parser;

import org.antlr.runtime.tree.Tree;

import com.nebrija.riif.model.Constant;
import com.nebrija.riif.model.RIIFElement;
import com.nebrija.riif.model.RIIFPropertiesContainer;

/**
 * Class to build a Constant object from the constant token in the tree
 * @author asanchep
 *
 */
public class ConstantElementProcessor implements RIIFParserProcessor{


		@Override
		public RIIFElement apply(RIIFElement container, Tree t) {
			if(! (container instanceof RIIFPropertiesContainer)){
				return null;
			}
			// Retrieve the id and type of the constant 
			String id = t.getChild(0).getText();
			String type = t.getChild(1).getText();
			// Create the constant and set the properties
			Constant co = new Constant(id);
			co.setProperty(Constant.TYPE_KEY, type);
			// If a value was defined, set the value
			if (t.getChildCount()>2){
				String value = t.getChild(2).getText();
				co.setValue(TypeBuilder.buildType(type, value));
 			}
			//Add the constant to the component 
			((RIIFPropertiesContainer)container).addConstant(co);
			return co;
		}

}
